//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.farm.wcp.controller;

import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.farm.core.page.ViewMode;
import com.farm.core.sql.result.DataResult;
import com.farm.doc.domain.FarmDocgroup;
import com.farm.doc.domain.ex.DocEntire;
import com.farm.doc.domain.ex.GroupEntire;
import com.farm.doc.exception.CanNoReadException;
import com.farm.doc.exception.DocNoExistException;
import com.farm.doc.exception.NoGroupAuthForLicenceException;
import com.farm.doc.server.FarmDocManagerInter;
import com.farm.doc.server.FarmDocOperateRightInter;
import com.farm.doc.server.FarmDocRunInfoInter;
import com.farm.doc.server.FarmDocgroupManagerInter;
import com.farm.doc.server.FarmDocgroupManagerInter.SearchType;
import com.farm.doc.server.FarmDocmessageManagerInter;
import com.farm.doc.server.FarmFileManagerInter;
import com.farm.doc.server.PubmessageServiceInter;
import com.farm.doc.server.UsermessageServiceInter;
import com.farm.wcp.know.service.KnowServiceInter;
import com.farm.wcp.util.ThemesUtil;
import com.farm.web.WebUtils;

@RequestMapping({"/webgroup"})
@Controller
public class DocGroupController extends WebUtils {
    @Resource
    private FarmDocgroupManagerInter farmDocgroupManagerImpl;
    @Resource
    private FarmFileManagerInter farmFileManagerImpl;
    @Resource
    private FarmDocManagerInter farmDocManagerImpl;
    @Resource
    private FarmDocRunInfoInter farmDocRunInfoImpl;
    @Resource
    private KnowServiceInter KnowServiceImpl;
    @Resource
    private FarmDocmessageManagerInter farmDocmessageManagerImpl;
    @Resource
    private FarmDocOperateRightInter farmDocOperateRightImpl;
    @Resource
    private UsermessageServiceInter usermessageServiceImpl;
    @Resource
    private PubmessageServiceInter pubmessageServiceImpl;
    private static final Logger log = Logger.getLogger(DocGroupController.class);

    public DocGroupController() {
    }

    @RequestMapping({"/add"})
    public ModelAndView add(HttpSession session, HttpServletRequest request) {
        return ViewMode.getInstance().returnModelAndView(ThemesUtil.getThemePath() + "/docgroup/editGroup");
    }

    @RequestMapping({"/edit"})
    public ModelAndView edit(String groupid, HttpSession session, HttpServletRequest request) {
        GroupEntire group = this.farmDocgroupManagerImpl.getFarmDocgroup(groupid);
        return ViewMode.getInstance().putAttr("group", group).returnModelAndView(ThemesUtil.getThemePath() + "/docgroup/editGroup");
    }

    @RequestMapping({"/userMag"})
    public ModelAndView userEdit(Integer page, String groupid, HttpSession session, HttpServletRequest request) {
        if (page == null) {
            page = Integer.valueOf(1);
        }

        GroupEntire group = this.farmDocgroupManagerImpl.getFarmDocgroup(groupid);
        DataResult result = this.farmDocgroupManagerImpl.getGroupUser(groupid, SearchType.yes, SearchType.none, SearchType.none, 1, 10);
        DataResult users = this.farmDocgroupManagerImpl.getGroupUser(groupid, SearchType.no, SearchType.none, SearchType.none, page.intValue(), 10);
        return ViewMode.getInstance().putAttr("applys", result).putAttr("group", group).putAttr("users", users).putAttr("groupid", groupid).returnModelAndView(ThemesUtil.getThemePath() + "/docgroup/groupAdminConsole");
    }

    @RequestMapping({"/addCommit"})
    public ModelAndView addCommit(FarmDocgroup group, HttpSession session) {
        try {
            if (group != null && group.getId() != null && group.getId().trim().length() <= 0) {
                group.setId((String)null);
            }

            group = this.farmDocgroupManagerImpl.creatDocGroup(group.getGroupname(), group.getGrouptag(), group.getGroupimg(), group.getJoincheck().equals("1"), group.getGroupnote(), this.getCurrentUser(session));
            return ViewMode.getInstance().putAttr("id", group.getId()).returnRedirectUrl("/webgroup/PubHome.html");
        } catch (NoGroupAuthForLicenceException var4) {
            return ViewMode.getInstance().setError(var4.toString()).returnModelAndView(ThemesUtil.getThemePath() + "/error");
        } catch (Exception var5) {
            return ViewMode.getInstance().setError(var5.toString()).returnModelAndView(ThemesUtil.getThemePath() + "/error");
        }
    }

    @RequestMapping({"/editCommit"})
    public ModelAndView editCommit(FarmDocgroup group, HttpSession session) {
        try {
            group.setPstate("1");
            this.farmDocgroupManagerImpl.editDocGroup(group.getId(), group.getGroupname(), group.getGrouptag(), group.getGroupimg(), group.getJoincheck().equals("1"), group.getGroupnote(), this.getCurrentUser(session));
            return ViewMode.getInstance().returnRedirectUrl("/webgroup/Pubshow.do?groupid=" + group.getId());
        } catch (Exception var4) {
            var4.printStackTrace();
            return ViewMode.getInstance().returnRedirectUrl("/webgroup/Pubshow.do?groupid=" + group.getId());
        }
    }

    @RequestMapping({"/homeeditCommit"})
    public ModelAndView homeeditCommit(String groupId, String docid, String text, String editNote, HttpSession session) {
        try {
            DocEntire doc = this.farmDocManagerImpl.getDoc(docid);
            doc.setTexts(text, this.getCurrentUser(session));
            this.farmDocManagerImpl.editDoc(doc, editNote, this.getCurrentUser(session));
            return ViewMode.getInstance().returnRedirectUrl("/webgroup/Pubshow.do?groupid=" + groupId);
        } catch (Exception var7) {
            var7.printStackTrace();
            return ViewMode.getInstance().returnRedirectUrl("/webgroup/Pubshow.do?groupid=" + groupId);
        }
    }

    @RequestMapping({"/homeedit"})
    public ModelAndView homeedit(String groupid, HttpSession session, HttpServletRequest request) {
        ViewMode mode = ViewMode.getInstance();

        try {
            FarmDocgroup group = this.farmDocgroupManagerImpl.getFarmDocgroupEntity(groupid);
            String homedocid = group.getHomedocid();
            DocEntire doc = this.farmDocManagerImpl.getDoc(homedocid);
            mode.putAttr("group", group).putAttr("doc", doc);
        } catch (Exception var8) {
            return ViewMode.getInstance().setError(var8.toString()).returnModelAndView(ThemesUtil.getThemePath() + "/error");
        }

        return mode.returnModelAndView(ThemesUtil.getThemePath() + "/docgroup/editHomePage");
    }

    @RequestMapping({"/Pubshow"})
    public ModelAndView show(Integer pagenum, String groupid, HttpSession session, HttpServletRequest request) {
        ViewMode mode = ViewMode.getInstance();

        try {
            GroupEntire docgroup = this.farmDocgroupManagerImpl.getFarmDocgroup(groupid);
            if (this.getCurrentUser(session) != null && this.farmDocgroupManagerImpl.isJoinGroupByUser(groupid, this.getCurrentUser(session).getId())) {
                if (this.farmDocgroupManagerImpl.isAuditing(groupid, this.getCurrentUser(session).getId())) {
                    return ViewMode.getInstance().setError("正在审核中").returnModelAndView(ThemesUtil.getThemePath() + "/error");
                }
            } else if (this.farmDocgroupManagerImpl.isJoinCheck(groupid)) {
                return ViewMode.getInstance().setError("用户没有小组的访问权限").returnModelAndView(ThemesUtil.getThemePath() + "/error");
            }

            if (docgroup == null || docgroup.getId() == null) {
                throw new RuntimeException("该知识小组不存在");
            }

            DocEntire doc = null;

            try {
                doc = this.farmDocManagerImpl.getDoc(docgroup.getHomedocid(), this.getCurrentUser(session));
            } catch (DocNoExistException | CanNoReadException var9) {
                return ViewMode.getInstance().setError("没有权限，或不存在").returnModelAndView(ThemesUtil.getThemePath() + "/error");
            }

            if (this.getCurrentUser(session) != null) {
                mode.putAttr("usergroup", this.farmDocgroupManagerImpl.getFarmDocgroupUser(docgroup.getId(), this.getCurrentUser(session).getId()));
            }

            int docnum = this.farmDocgroupManagerImpl.getGroupDocNum(groupid);
            mode.putAttr("docs", this.farmDocgroupManagerImpl.getNewGroupDoc(this.getCurrentUser(session) == null ? null : this.getCurrentUser(session), groupid, 10, pagenum == null ? 1 : pagenum.intValue()));
            mode.putAttr("docnum", docnum).putAttr("home", doc).putAttr("groupid", groupid).putAttr("docgroup", docgroup);
        } catch (Exception var10) {
            log.error(var10 + "(groupid:" + groupid + ")");
            return ViewMode.getInstance().setError(var10.getMessage()).returnModelAndView(ThemesUtil.getThemePath() + "/error");
        }

        return mode.returnModelAndView(ThemesUtil.getThemePath() + "/docgroup/groupHome");
    }

    @RequestMapping(
        value = {"/PubHome"},
        method = {RequestMethod.GET}
    )
    public ModelAndView showDoc(Integer pagenum, HttpSession session, HttpServletRequest request) throws Exception {
        if (pagenum == null) {
            pagenum = Integer.valueOf(1);
        }

        DataResult groups = this.farmDocgroupManagerImpl.getGroups(12, pagenum);
        return ViewMode.getInstance().putAttr("groups", groups).returnModelAndView(ThemesUtil.getThemePath() + "/docgroup/groupIndex");
    }

    @RequestMapping({"/wipeAdmin"})
    @ResponseBody
    public Map<String, Object> groupWipeAdmin(String groupUserId, HttpSession session) {
        try {
            this.farmDocgroupManagerImpl.wipeAdminFromGroup(groupUserId, this.getCurrentUser(session));
        } catch (Exception var4) {
            return ViewMode.getInstance().setError(var4.getMessage()).returnObjMode();
        }

        return ViewMode.getInstance().returnObjMode();
    }

    @RequestMapping({"/groupSetAdmin"})
    @ResponseBody
    public Map<String, Object> groupSetAdmin(String groupUserId, HttpSession session) {
        try {
            this.farmDocgroupManagerImpl.setAdminForGroup(groupUserId, this.getCurrentUser(session));
        } catch (Exception var4) {
            return ViewMode.getInstance().setError(var4.getMessage()).returnObjMode();
        }

        return ViewMode.getInstance().returnObjMode();
    }

    @RequestMapping({"/groupWipeEditor"})
    @ResponseBody
    public Map<String, Object> groupWipeEditor(String groupUserId, HttpSession session) {
        try {
            this.farmDocgroupManagerImpl.wipeEditorForGroup(groupUserId, this.getCurrentUser(session));
        } catch (Exception var4) {
            return ViewMode.getInstance().setError(var4.getMessage()).returnObjMode();
        }

        return ViewMode.getInstance().returnObjMode();
    }

    @RequestMapping({"/groupSetEditor"})
    @ResponseBody
    public Map<String, Object> groupSetEditor(String groupUserId, HttpSession session) {
        try {
            this.farmDocgroupManagerImpl.setEditorForGroup(groupUserId, this.getCurrentUser(session));
        } catch (Exception var4) {
            return ViewMode.getInstance().setError(var4.getMessage()).returnObjMode();
        }

        return ViewMode.getInstance().returnObjMode();
    }

    @RequestMapping({"/groupQuit"})
    @ResponseBody
    public Map<String, Object> groupQuit(String groupUserId, HttpSession session) {
        try {
            this.farmDocgroupManagerImpl.leaveGroup(groupUserId, this.getCurrentUser(session));
        } catch (Exception var4) {
            return ViewMode.getInstance().setError(var4.getMessage()).returnObjMode();
        }

        return ViewMode.getInstance().returnObjMode();
    }

    @RequestMapping({"/agreeApply"})
    @ResponseBody
    public Map<String, Object> agreeApply(String groupUserId, HttpSession session) {
        try {
            this.farmDocgroupManagerImpl.agreeJoinApply(groupUserId, this.getCurrentUser(session));
        } catch (Exception var4) {
            return ViewMode.getInstance().setError(var4.getMessage()).returnObjMode();
        }

        return ViewMode.getInstance().returnObjMode();
    }

    @RequestMapping({"/refuseApply"})
    @ResponseBody
    public Map<String, Object> refuseApply(String groupUserId, HttpSession session) {
        try {
            this.farmDocgroupManagerImpl.refuseJoinApply(groupUserId, this.getCurrentUser(session));
        } catch (Exception var4) {
            return ViewMode.getInstance().setError(var4.getMessage()).returnObjMode();
        }

        return ViewMode.getInstance().returnObjMode();
    }

    @RequestMapping({"/haveAdministratorIs"})
    @ResponseBody
    public Map<String, Object> haveAdministratorIs(String groupId) {
        int adminNum = this.farmDocgroupManagerImpl.getAllAdministratorByGroup(groupId).size();
        return ViewMode.getInstance().putAttr("adminNum", adminNum).returnObjMode();
    }

    @RequestMapping({"/leaveGroup"})
    public ModelAndView leaveGroup(String groupId, HttpSession session) {
        this.farmDocgroupManagerImpl.leaveGroup(groupId, this.getCurrentUser(session).getId());
        return ViewMode.getInstance().returnRedirectUrl("/webgroup/Pubshow.do?groupid=" + groupId);
    }

    @RequestMapping({"/join"})
    public ModelAndView joinform(String groupId, HttpSession session, HttpServletRequest request) throws Exception {
        if (this.farmDocgroupManagerImpl.isJoinGroupByUser(groupId, this.getCurrentUser(session).getId())) {
            if (this.farmDocgroupManagerImpl.isAuditing(groupId, this.getCurrentUser(session).getId())) {
                return ViewMode.getInstance().setError("正在审核中").returnModelAndView(ThemesUtil.getThemePath() + "/error");
            }
        } else {
            if (this.farmDocgroupManagerImpl.isJoinCheck(groupId)) {
                return ViewMode.getInstance().putAttr("group", this.farmDocgroupManagerImpl.getFarmDocgroupEntity(groupId)).returnModelAndView(ThemesUtil.getThemePath() + "/docgroup/joinCheckForm");
            }

            this.farmDocgroupManagerImpl.applyGroup(groupId, this.getCurrentUser(session).getId(), "申请加入", this.getCurrentUser(session));
        }

        return ViewMode.getInstance().putAttr("groupid", groupId).returnRedirectUrl("/webgroup/Pubshow.do");
    }

    @RequestMapping({"/joincommit"})
    public ModelAndView joincommit(String groupId, String message, HttpSession session, HttpServletRequest request) throws Exception {
        if (this.farmDocgroupManagerImpl.isJoinGroupByUser(groupId, this.getCurrentUser(session).getId())) {
            return this.farmDocgroupManagerImpl.isAuditing(groupId, this.getCurrentUser(session).getId()) ? ViewMode.getInstance().setError("正在审核中").returnModelAndView(ThemesUtil.getThemePath() + "/error") : ViewMode.getInstance().putAttr("groupid", groupId).returnRedirectUrl("/webgroup/Pubshow.do");
        } else {
            this.farmDocgroupManagerImpl.applyGroup(groupId, this.getCurrentUser(session).getId(), message, this.getCurrentUser(session));
            FarmDocgroup group = this.farmDocgroupManagerImpl.getFarmDocgroupEntity(groupId);
            return ViewMode.getInstance().setError("已经提交加入请求，请等待管理员审核!").putAttr("GROUP", group).returnModelAndView(ThemesUtil.getThemePath() + "/message");
        }
    }
}
